package com.ys.controller.api.amazonorder;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.ys.config.MenuId;
import com.ys.entity.amazonorder.TpAmazonShopOrderDaily;
import com.ys.entity.sys.YsCol;
import com.ys.enume.sys.YsColEnum;
import com.ys.service.sys.CommonApiService;
import com.ys.service.sys.UploadService.UploadDto;
import com.ys.util.DateUtil;
import com.ys.util.ExportExcelUtils;
import com.ys.util.admin.ConditionUtil;
import com.ys.vo.LayuiTableVoJA;
import com.ys.vo.R;

/**
 * 商品日报表
 *
 */
@Controller
@RequestMapping("/api/inter/tpAmazonShopOrderDaily")
public class ApiInterTpAmazonShopOrderDailyController {
	
//	private static final Logger logger = Logger.getLogger(ApiAdminLoginController.class);
	
	@Autowired
	private CommonApiService<TpAmazonShopOrderDaily> commonApiService;

	@RequiresPermissions("tpAmazonShopOrderDaily:view")
	@RequestMapping("/search")
    @ResponseBody
	public LayuiTableVoJA search(
			@ModelAttribute TpAmazonShopOrderDaily cond,
			String conditions){
		cond = commonApiService.dataAuth(MenuId.TP_AmazonShopOrderDaily, cond);
		
		String dayPurchase = ConditionUtil.getConditionValue(conditions, "dayPurchase");
		
		if (dayPurchase == null || dayPurchase.length() == 0) {
			Date now = new Date();
			cond.where("A.DayPurchase >= '" + DateUtil.getYYYY_MM_DD(DateUtil.addDays(now, -1)) + "'")
				.where("A.DayPurchase <= '" + DateUtil.getYYYY_MM_DD(now) + "'");
		}
		
		cond.setOrderby("A.DayPurchase desc,A.Num desc");
		
		LayuiTableVoJA vo = commonApiService.searchLay(cond);
		{
			JSONObject JO = new JSONObject();
			
			// 订单数累计
			JO.put("num", cond.setFields("ISNULL(SUM(A.Num),0)").sumDecimal());
			
			// 订单总金额累计
			JO.put("amountRmb", cond.setFields("ISNULL(SUM(A.AmountRmb),0)").sumDecimal());

			vo.setTotalRow(JO);
		}
		return vo;
	}
	
	@RequestMapping("/export")
    @ResponseBody
    public R export(@ModelAttribute TpAmazonShopOrderDaily cond, String conditions) throws Exception{

		cond = commonApiService.dataAuth(MenuId.TP_AmazonShopOrderDaily, cond);
		
		cond.setPage(0).setSize(999999);
		
		String dayPurchases = ConditionUtil.getConditionValue(conditions, "dayPurchase");
		
		if (dayPurchases != null && dayPurchases.length() > 0) {
			
			cond.setOrderby("A.DayPurchase desc");
			
		
			List<Map<String, Object>> listData = cond.queryMap();
	        
	    	List<YsCol> listField = new YsCol()
	    			.setMenuId(MenuId.TP_AmazonShopOrderDaily)
	    			.setFrontShow(YsColEnum.FrontShow.SHOW.value)
	    			.setOrderby("A.SortNo asc")
	    			.queryList();
	    	
	        ExportExcelUtils<TpAmazonShopOrderDaily> util = new ExportExcelUtils<TpAmazonShopOrderDaily>(new TpAmazonShopOrderDaily());
	        
	        String toFilePath = util.exportExcel(listData, listField, "商品日报表");
	        UploadDto dto = commonApiService.uploadLocalToServer(toFilePath);
	        return R.ok(dto.getUrl());
		} else {
	        return R.error("必须指定时间范围");
		}
    }
}
